﻿using System.Web.Mvc;
using System.Web.Security;
using CodeStash.Filters;
using CodeStash.Models.Security;
using CodeStash.Models.Settings;
using CodeStash.Models.Snippet;
using CodeStash.Services;
using System;
using System.Linq;

namespace CodeStash.Controllers
{
    public class SettingsController : BaseTagCloudEnabledController
    {
        private readonly ILoggerService loggerService;
        private readonly IMembershipService membershipService;




        public SettingsController(
            ILoggerService loggerService, 
            IMembershipService membershipService,
            ITagCloudService tagCloudService)
            : base(tagCloudService)
        {
            this.loggerService = loggerService;
            this.membershipService = membershipService;
        }


        [Authorize]
        [RenderTagCloud]
        [HttpGet]
        public ActionResult Index()
        {

            if (User.Identity.IsAuthenticated)
            {
                loggerService.Info("Settings : User is autenticated attempting to get UserProfile");
                MembershipUser user = membershipService.GetUserByUserName(User.Identity.Name);
                UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(User.Identity.Name);
                ChangeSettingsModel vm = new ChangeSettingsModel();
                vm.HighlightingCSSList = ChangeSettingsModel.CompleteHighlightingCSSList.Value;
                vm.HighlightingCSS =
                    profile.HighlightingCSSId == 0 ? 23 : profile.HighlightingCSSId;
                vm.MaxSnippetsToDisplay = 
                    profile.MaxSnippetsToDisplay == 0 ? DisplaySnippetsViewModel.MAX_SNIPPETS_TO_DISPLAY : profile.MaxSnippetsToDisplay;
                return View(vm);
            }
            else
            {
                loggerService.Error("Settings : User is NOT autenticated, redirected to Logon view");
                return RedirectToAction("Index", "Home");
            }

        }


        [Authorize]
        [HttpPost]
        [AjaxOnly]
        [ValidateAntiForgeryToken(Salt = "ChangeSettings")]
        public ActionResult ChangeSettings(ChangeSettingsModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    MembershipUser user = membershipService.GetUserByUserName(User.Identity.Name);
                    UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(User.Identity.Name);
                    profile.HighlightingCSSId = vm.HighlightingCSS;
                    profile.MaxSnippetsToDisplay = vm.MaxSnippetsToDisplay;
                    DisplaySnippetsViewModel.MAX_SNIPPETS_TO_DISPLAY =
                            vm.MaxSnippetsToDisplay > 0 ? vm.MaxSnippetsToDisplay : 100;
                    loggerService.Info(string.Format("Settings : Applying UserProfile settings HighlightingCSSId: {0} MaxSnippetsToDisplay: {1}",
                            profile.HighlightingCSSId, profile.MaxSnippetsToDisplay));
                    profile.Save();
                    ViewData["successfulEdit"] = true;
                    vm.HighlightingCSSList = ChangeSettingsModel.CompleteHighlightingCSSList.Value;
                    return PartialView("ChangeSettingsPartial", vm);
                }
                else
                {
                    string errors = ModelState.Keys.Aggregate((x, y) => string.Format("{0}\r\n{1}", x, y));
                    loggerService.Error(string.Format("Settings : The settings model was invalid\r\nError keys were\r\n{0}", errors));
                    ViewData["successfulEdit"] = false;
                    vm.HighlightingCSSList = ChangeSettingsModel.CompleteHighlightingCSSList.Value;
                    return PartialView("ChangeSettingsPartial", vm);
                }
            }
            catch(Exception ex)
            {
                loggerService.Error(string.Format("Settings : The settings model was invalid. Message :{0}, StackTrace: {1}", ex.Message, ex.StackTrace));
                vm.HighlightingCSSList = ChangeSettingsModel.CompleteHighlightingCSSList.Value;
                ViewData["successfulEdit"] = false;
                return PartialView("ChangeSettingsPartial", vm);
            }
        }



        


    }
}
